package com.rc.evcharger.service.handle;

import com.rc.evcharger.common.constant.SocketConstant;
import com.rc.evcharger.common.dto.CalcalateResultDto;
import com.rc.evcharger.common.dto.CalculateDto;
import com.rc.evcharger.model.elec.ElecPile;
import com.rc.evcharger.model.elec.ElecStationPrice;
import com.rc.evcharger.redis.RedChargeDataService;
import com.rc.evcharger.service.elec.ElecPileService;
import com.rc.evcharger.service.elec.ElecStationPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * Create by Yang on 2021/11/27
 */
@Component
public class ChargeHandler {

    @Autowired
    private ElecPileService elecPileService;

    @Autowired
    private ElecStationPriceService elecStationPriceService;

    @Autowired
    private RedChargeDataService redChargeDataService;


    public CalcalateResultDto calculateAmount(CalculateDto calculateDto) {
        CalcalateResultDto calcalateResultDto = new CalcalateResultDto();

        //过渡使用
        if (calculateDto.getServicePrice() == null) {
            ElecStationPrice elecStationPrice = elecStationPriceService.selectByPrimaryKey(calculateDto.getElecStationPriceId());
            calculateDto.setElecPrice(elecStationPrice.getElecPrice());
            calculateDto.setServicePrice(elecStationPrice.getServicePrice());
        }
        ElecPile elecPile = elecPileService.selectByPrimaryKey(calculateDto.getElecPileId());

        //获取电量并存入redis中;
        BigDecimal lastPowerValue = redChargeDataService.getPower(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getOrderNum());
        //if (lastPowerValue.intValue() == 0){
        //lastPowerValue = redChargeDataService.getPower(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getMobile());
        //}
        redChargeDataService.putPower(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getOrderNum(), calculateDto.getPower());

        //redChargeDataService.putPower(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getMobile(), calculateDto.getPower());
        calculateDto.setPowerValue(lastPowerValue);
        calcalateResultDto.setLastPower(lastPowerValue);

        //防止被挂起订单的数据覆盖，如果是遗留数据则清零
        if (calculateDto.getPower().compareTo(BigDecimal.ZERO) <= 0) {
            redChargeDataService.putPower(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getOrderNum(), SocketConstant.CON0);
            redChargeDataService.putBaseAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getOrderNum(), SocketConstant.CON0);
            redChargeDataService.putServiceAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getOrderNum(), SocketConstant.CON0);
            redChargeDataService.putAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getOrderNum(), SocketConstant.CON0);

            //redChargeDataService.putPower(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getMobile(), SocketConstant.CON0);
            //redChargeDataService.putBaseAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getMobile(), SocketConstant.CON0);
            //redChargeDataService.putServiceAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getMobile(), SocketConstant.CON0);
            //redChargeDataService.putAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getMobile(), SocketConstant.CON0);

            calcalateResultDto.setElecAmount(BigDecimal.ZERO);
            calcalateResultDto.setServiceAmount(BigDecimal.ZERO);
            calcalateResultDto.setTotalAmount(BigDecimal.ZERO);

            //获取上次实时电费、服务费信息
            calcalateResultDto.setLastServiceAmount(BigDecimal.ZERO);
            calcalateResultDto.setLastElecAmount(BigDecimal.ZERO);
            calcalateResultDto.setLastTotalAmount(BigDecimal.ZERO);
            return calcalateResultDto;
        }

        //得到实时的电量;
        BigDecimal realPower = null;
        if (calculateDto.getPowerValue().compareTo(BigDecimal.ZERO) == 0) {
            realPower = calculateDto.getPower();
        } else {
            realPower = calculateDto.getPower().subtract(calculateDto.getPowerValue());
        }
        /**总电价,基本电费,服务费**/
        BigDecimal elecAmount = null;
        BigDecimal serviceAmount = null;
        BigDecimal totalAmount = null;

        BigDecimal lastBaseAmount = redChargeDataService.getBaseAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getOrderNum());
        BigDecimal lastServiceAmount = redChargeDataService.getServiceAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getOrderNum());
        //新key为空，则取值旧key
        //if (lastBaseAmount.intValue() == 0 || lastServiceAmount.intValue() == 0){
        //    lastBaseAmount = redChargeDataService.getBaseAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getMobile());
        //    lastServiceAmount = redChargeDataService.getServiceAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getMobile());
        //}

        if (lastBaseAmount == null || lastBaseAmount.equals("")) {
            /**计算得到基本电费与服务费**/
            elecAmount = calculateDto.getElecPrice().multiply(calculateDto.getPower()).setScale(5, BigDecimal.ROUND_HALF_UP);
            serviceAmount = calculateDto.getServicePrice().multiply(calculateDto.getPower()).setScale(5, BigDecimal.ROUND_HALF_UP);
            totalAmount = elecAmount.add(serviceAmount);

            //获取上次实时电费、服务费信息
            calcalateResultDto.setLastServiceAmount(BigDecimal.ZERO);
            calcalateResultDto.setLastElecAmount(BigDecimal.ZERO);
            calcalateResultDto.setLastTotalAmount(BigDecimal.ZERO);
        } else {
            /**计算得到基本电费与服务费**/
            elecAmount = calculateDto.getElecPrice().multiply(realPower).add(lastBaseAmount).setScale(5,
                    BigDecimal.ROUND_HALF_UP);
            serviceAmount = calculateDto.getServicePrice().multiply(realPower).add(lastServiceAmount).setScale(5,
                    BigDecimal.ROUND_HALF_UP);
            totalAmount = elecAmount.add(serviceAmount);

            //获取上次实时电费、服务费信息
            calcalateResultDto.setLastServiceAmount(lastServiceAmount.setScale(2, BigDecimal.ROUND_HALF_UP));
            calcalateResultDto.setLastElecAmount(lastBaseAmount.setScale(2, BigDecimal.ROUND_HALF_UP));
            calcalateResultDto.setLastTotalAmount(calcalateResultDto.getLastElecAmount().add(calcalateResultDto.getLastServiceAmount()));
        }
        /**保存基本电费与服务费,总价格**/
        redChargeDataService.putBaseAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getOrderNum(), elecAmount.toString());
        redChargeDataService.putServiceAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getOrderNum(), serviceAmount.toString());
        redChargeDataService.putAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getOrderNum(), totalAmount.toString());
        /** 旧 保存基本电费与服务费,总价格**/
        //redChargeDataService.putBaseAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getMobile(), elecAmount.toString());
        //redChargeDataService.putServiceAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getMobile(), serviceAmount.toString());
        //redChargeDataService.putAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getMobile(), totalAmount.toString());


        calcalateResultDto.setElecAmount(elecAmount);
        calcalateResultDto.setServiceAmount(serviceAmount);
        calcalateResultDto.setTotalAmount(totalAmount);
        return calcalateResultDto;
    }

    /**
     * 取实时数据
     *
     * @param calculateDto
     * @return
     */
    public CalcalateResultDto calculateAmountNotPut(CalculateDto calculateDto) {

        ElecStationPrice elecStationPrice = elecStationPriceService.selectByPrimaryKey(calculateDto.getElecStationPriceId());
        ElecPile elecPile = elecPileService.selectByPrimaryKey(calculateDto.getElecPileId());

        //得到实时的电量;
        BigDecimal realPower = null;
        if (calculateDto.getPowerValue().compareTo(BigDecimal.ZERO) == 0) {
            realPower = calculateDto.getPower();
        } else {
            realPower = calculateDto.getPower().subtract(calculateDto.getPowerValue());
        }
        /**总电价,基本电费,服务费**/
        BigDecimal elecAmount = null;
        BigDecimal serviceAmount = null;
        BigDecimal totalAmount = null;


        BigDecimal basePriceValue = redChargeDataService.getBaseAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getOrderNum());
        BigDecimal servicePriceValue = redChargeDataService.getServiceAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getOrderNum());

        if (basePriceValue == null || basePriceValue.equals("")) {
            /**计算得到基本电费与服务费**/
            elecAmount = elecStationPrice.getElecPrice().multiply(calculateDto.getPower()).setScale(5, BigDecimal.ROUND_HALF_UP);
            serviceAmount = elecStationPrice.getServicePrice().multiply(calculateDto.getPower()).setScale(5, BigDecimal.ROUND_HALF_UP);
            totalAmount = elecAmount.add(serviceAmount);
        } else {
            /**计算得到基本电费与服务费**/
            elecAmount = elecStationPrice.getElecPrice().multiply(realPower).add(basePriceValue).setScale(5,
                    BigDecimal.ROUND_HALF_UP);
            serviceAmount = elecStationPrice.getServicePrice().multiply(realPower).add(servicePriceValue).setScale(5,
                    BigDecimal.ROUND_HALF_UP);
            totalAmount = elecAmount.add(serviceAmount);
        }
        /**保存基本电费与服务费,总价格**/
        //redChargeDataService.putBaseAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getMobile(), elecAmount.toString());
        //redChargeDataService.putServiceAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getMobile(), serviceAmount.toString());
        //redChargeDataService.putAmount(elecPile.getPileRtua(), calculateDto.getGunNum(), calculateDto.getMobile(), totalAmount.toString());

        CalcalateResultDto calcalateResultDto = new CalcalateResultDto();
        calcalateResultDto.setElecAmount(elecAmount);
        calcalateResultDto.setServiceAmount(serviceAmount);
        calcalateResultDto.setTotalAmount(totalAmount);
        return calcalateResultDto;
    }
}
